package com.heima.admin.aop;

import com.heima.admin.service.IAdUserLoginService;
import com.heima.admin.service.IAdUserOpertionService;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.pojos.AdUserLogin;
import com.heima.model.admin.pojos.AdUserOpertion;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.AdThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@Aspect
@Component
public class LogAspect {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IAdUserOpertionService adUserOpertionService;
    @Autowired
    private IAdUserLoginService userLoginService;

    /**
     * 拦截管理员登录
     */
    @Pointcut("execution(* com.heima.admin.controller.v1.AdUserController.login(..))")
    public void excludeLoginControllerMethods() {
    }

    /**
     * 拦截所有接口
     */
    @Pointcut("execution(* com.heima.admin.controller.v1..*(..))")
    public void excludeControllerMethods() {
    }

    /**
     * 拦截除登录接口外的所有接口
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("excludeControllerMethods() && !excludeLoginControllerMethods()")
    public Object logAroundOtherControllers(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取方法类型
        String requestMethod = request.getMethod();
        if ("GET".equals(requestMethod)) {
            return joinPoint.proceed();
        }
        AdUserOpertion adUserOpertion = new AdUserOpertion();
        adUserOpertion.setType(1);
        //
        Object res = joinPoint.proceed();
        //获取登录Ip
        String ip = request.getRemoteAddr();
        adUserOpertion.setIp(ip);
        adUserOpertion.setUserId(AdThreadLocalUtil.getUser().getId());
        adUserOpertion.setCreatedTime(LocalDateTime.now());
        adUserOpertionService.save(adUserOpertion);
        return res;
    }

    /**
     * 拦截登录接口
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("excludeLoginControllerMethods()")
    public Object logAroundLoginController(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        ResponseResult responseResult = (ResponseResult) res;
        Map<String, Object> map = (Map<String, Object>) responseResult.getData();
        AdUser adUser = (AdUser) map.get("user");
        //获取登录Ip
        String ip = request.getRemoteAddr();
        AdUserLogin adUserLogin = new AdUserLogin();
        adUserLogin.setUserId(adUser.getId());
        adUserLogin.setIp(ip);
        adUserLogin.setCreatedTime(LocalDateTime.now());
        userLoginService.save(adUserLogin);
        return res;
    }

}
